package com.jinglin.admin.controller;

import com.aliyun.oss.model.OSSObject;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.enums.DateTimePatternEnum;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.utils.DateUtil;
import com.jinglin.utils.OssUtils;
import org.apache.coyote.Response;
import org.apache.ibatis.annotations.Param;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

@RestController
@RequestMapping("/file")
@Validated
public class FileController extends ABaseController {
    @Autowired
    private OssUtils ossUtils;
    @RequestMapping("/uploadImage")
    public ResponseVO uploadImage(@NotNull MultipartFile file,@NotNull Boolean createThumbnail){
        String s = ossUtils.uploadImage(file,"easylive1");
        return getSuccessResponseVO(s);
    }
    @RequestMapping("/getResource")
    public void getResource(@Param("sourceName")String sourceName, HttpServletResponse response) throws IOException {
        OSSObject ossObject = ossUtils.getObject("easylive1", sourceName);
        response.setContentType("application/octet-stream");
        try(
                InputStream inp = ossObject.getObjectContent();
                OutputStream out = response.getOutputStream()
        ){
            byte[] bytes = new byte[1024];
            int bytesRead;
            while ((bytesRead = inp.read(bytes)) != -1){
                out.write(bytes,0,bytesRead);
            }
            out.flush();
        }
    }

}
